{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU",
    "gpuClass": "standard"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "Clone webui repository"
      ],
      "metadata": {
        "id": "OHCtG9MT0jLq"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui\n",
        "%cd stable-diffusion-webui"
      ],
      "metadata": {
        "id": "sBbcB4vwj_jm",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "7d5fbd3c-fca4-4223-e0b0-4b7c34996d6f"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Cloning into 'stable-diffusion-webui'...\n",
            "remote: Enumerating objects: 4564, done.\u001b[K\n",
            "remote: Counting objects: 100% (143/143), done.\u001b[K\n",
            "remote: Compressing objects: 100% (74/74), done.\u001b[K\n",
            "remote: Total 4564 (delta 84), reused 104 (delta 69), pack-reused 4421\u001b[K\n",
            "Receiving objects: 100% (4564/4564), 20.39 MiB | 11.05 MiB/s, done.\n",
            "Resolving deltas: 100% (3192/3192), done.\n",
            "/content/stable-diffusion-webui\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Download the model from NovelAILeaks."
      ],
      "metadata": {
        "id": "F0EINk5M0s-w"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!mkdir -p /content/stable-diffusion-webui/models/Stable-diffusion /content/stable-diffusion-webui/models/hypernetworks\n",
        "%cd /content/stable-diffusion-webui/models/Stable-diffusion/\n",
        "\n",
        "# 7G animefull-final-latest (may not work)\n",
        "# !gdown 17WWd6KEsBj7D_0TyGp8aXHQDlchYVs1a -O /content/stable-diffusion-webui/models/Stable-diffusion/model.ckpt\n",
        "\n",
        "# 4G animefull-final-pruned\n",
        "# !gdown 1d3f2fvN2gLRocBahZrXe_v1EEHuqpUzT -O /content/stable-diffusion-webui/models/Stable-diffusion/model.ckpt\n",
        "\n",
        "# 4G animefull-final-pruned (backup)\n",
        "!curl -Lo model.ckpt https://cloudflare-ipfs.com/ipfs/bafybeicpamreyp2bsocyk3hpxr7ixb2g2rnrequub3j2ahrkdxbvfbvjc4/model.ckpt\n",
        "\n",
        "# Install VAE Weights (optional)\n",
        "!curl -Lo model.vae.pt https://cloudflare-ipfs.com/ipfs/bafybeiccldswdd3wvg57jhclcq53lvsc6gizasiblwayvhlv6eq4wow7wu/animevae.pt \n",
        "\n",
        "# Install hypernetwork （optional)\n",
        "!curl -L https://cloudflare-ipfs.com/ipfs/bafybeiduanx2b3mcvxlwr66igcwnpfmk3nc3qgxlpwh6oq6m6pxii3f77e/_modules.tar | tar x -C /content/stable-diffusion-webui/models/hypernetworks\n",
        "\n",
        "# Install custom embeddings (modified, optional)\n",
        "# !curl -L https://cloudflare-ipfs.com/ipfs/bafybeie3hdjchxs5tz4n75bos53nhcklslguxchdurc2ynrzcfv2kwyklu/embeddings.tar | tar x -C /content/stable-diffusion-webui/embeddings"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ZzRNMT42Gw_p",
        "outputId": "5f256bc3-ce49-4444-e232-72ecbe282330"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "/content/stable-diffusion-webui/models/Stable-diffusion\n",
            "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
            "                                 Dload  Upload   Total   Spent    Left  Speed\n",
            "100 4067M  100 4067M    0     0   111M      0  0:00:36  0:00:36 --:--:--  145M\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Launch web ui. You will get a link to nnn.gradio.app, follow it.\n",
        "\n",
        "Commandline arguments are:\n",
        "  - `--share` - create online gradio.app link\n",
        "  - `--gradio-debug` - print outputs to console\n",
        "  - `--gradio-auth me:qwerty` - add authentication to gradio: username me, password qwerty"
      ],
      "metadata": {
        "id": "xt8lbdmC04ox"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "%cd /content/stable-diffusion-webui\n",
        "!COMMANDLINE_ARGS=\"--share --gradio-debug --medvram\" REQS_FILE=\"requirements.txt\" python launch.py"
      ],
      "metadata": {
        "id": "R-xAdMA5wxXd",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "9e234b7e-95f1-4456-d0ed-483989838fef"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "/content/stable-diffusion-webui\n",
            "Python 3.7.14 (default, Sep  8 2022, 00:06:44) \n",
            "[GCC 7.5.0]\n",
            "Commit hash: 97bc0b9504572d2df80598d0b694703bcd626de6\n",
            "Installing requirements for Web UI\n",
            "Launching Web UI with arguments: --share --gradio-debug --medvram\n",
            "LatentDiffusion: Running in eps-prediction mode\n",
            "DiffusionWrapper has 859.52 M params.\n",
            "making attention of type 'vanilla' with 512 in_channels\n",
            "Working with z of shape (1, 4, 32, 32) = 4096 dimensions.\n",
            "making attention of type 'vanilla' with 512 in_channels\n",
            "Loading weights [925997e9] from /content/stable-diffusion-webui/models/Stable-diffusion/model.ckpt\n",
            "Model loaded.\n",
            "Loaded a total of 0 textual inversion embeddings.\n",
            "Running on local URL:  http://127.0.0.1:7860\n",
            "Running on public URL: https://13759.gradio.app\n",
            "\n",
            "This share link expires in 72 hours. For free permanent hosting, check out Spaces: https://huggingface.co/spaces\n",
            "  0% 0/20 [00:00<?, ?it/s]\n",
            "  5% 1/20 [00:04<01:29,  4.71s/it]\n",
            " 10% 2/20 [00:04<00:37,  2.08s/it]\n",
            " 15% 3/20 [00:05<00:21,  1.24s/it]\n",
            " 20% 4/20 [00:05<00:13,  1.18it/s]\n",
            " 25% 5/20 [00:05<00:09,  1.59it/s]\n",
            " 30% 6/20 [00:05<00:06,  2.01it/s]\n",
            " 35% 7/20 [00:06<00:05,  2.42it/s]\n",
            " 40% 8/20 [00:06<00:04,  2.79it/s]\n",
            " 45% 9/20 [00:06<00:03,  3.11it/s]\n",
            " 50% 10/20 [00:06<00:02,  3.37it/s]\n",
            " 55% 11/20 [00:07<00:02,  3.58it/s]\n",
            " 60% 12/20 [00:07<00:02,  3.73it/s]\n",
            " 65% 13/20 [00:07<00:01,  3.85it/s]\n",
            " 70% 14/20 [00:07<00:01,  3.93it/s]\n",
            " 75% 15/20 [00:08<00:01,  4.01it/s]\n",
            " 80% 16/20 [00:08<00:00,  4.05it/s]\n",
            " 85% 17/20 [00:08<00:00,  4.08it/s]\n",
            " 90% 18/20 [00:08<00:00,  4.10it/s]\n",
            " 95% 19/20 [00:09<00:00,  4.11it/s]\n",
            "100% 20/20 [00:09<00:00,  2.15it/s]\n",
            "Total progress: 100% 20/20 [00:05<00:00,  3.52it/s]\n",
            "Interrupted with signal 2 in <frame at 0x7fe77eaa9230, file '/usr/local/lib/python3.7/dist-packages/gradio/blocks.py', line 1238, code block_thread>\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "commands for ***after*** you have gotten done with a session\n",
        "============================================================================"
      ],
      "metadata": {
        "id": "T3957x2AY7iP"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Zip images for downloading on local drive (click the folder icon on the left, the one below {x})"
      ],
      "metadata": {
        "id": "aCtJffM2ZE06"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!zip -r /content/stable-diffusion-webui /content/stable-diffusion-webui/outputs "
      ],
      "metadata": {
        "id": "TmRqNyiAZCHu"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Save images to Google Drive **Warning: this will cause google to scan your drive, so if you intend to use this and worry about that kind of stuff, probablly just set this up on a clean account that's just for this colab**"
      ],
      "metadata": {
        "id": "tcslU-S2ZNr9"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from google.colab import drive # type: ignore\n",
        "\n",
        "try:\n",
        "   drive_path = \"/content/drive\"\n",
        "   drive.mount(drive_path,force_remount=False)\n",
        "except:\n",
        "   print(\"...error mounting drive or with drive path variables\")\n",
        "\n",
        "!cp -r \"/content/stable-diffusion-webui/outputs\" \"/content/drive/MyDrive\""
      ],
      "metadata": {
        "id": "-N0lnu-TZOTW"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}
